<?php
/*
 * @Date: 2021-01-30 21:23:46
 * @名称: 漫画信息 - 增加用户和审核
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-03-01 20:26:33
 * @FilePath: /Site/app/useradmin/controller/Comics/User/AddUsersAndConfirmations.php
 */

namespace app\useradmin\controller\Comics\User;

use app\BaseController;
use app\useradmin\controller\DataBaseController\AuthorAndChineseTeam;
use app\useradmin\controller\DataBaseController\LogController;
use think\facade\Db;

class AddUsersAndConfirmations extends BaseController
{
    /**
     * @方法名: 添加用户
     * @作者: 初雪桜
     * @信息: 作者/协作者 添加用户管理漫画
     * @param Json $user_info
     * @param Json $postData
     * @return Json
     */
    public function AddUser($user_info, $postData) //漫画所有者添加不需要审核 如果是 协作者添加需要审核
    {
        $log_book_id = null;
        $log_user_id = null;
        $log_new_user_id = null;
        $log__id = null;
        $log_msg = null;
        $log_state = null;
        $log_apply_user_id = null;
        $implement_time = null;
        $implement_user_id = null;
        //由于 关闭了原用户的验证现在重新验证
        $tempHashHmac = hash_hmac('sha256', $postData['jsonDecodeinitListUserInfo']['user_id'] . $postData['bookId'] . $postData['jsonDecodeinitListUserInfo']['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
        if ($tempHashHmac != $postData['jsonDecodeinitListUserInfo']['hash_hmac_user_id']) { //判断是不是 加密的信息和解析出来的用户信息一致
            return json_decode(errorJsonReturn("AuthorAndChineseTeam_EnterAddUser-User-10000")->getContent(), true);
        }
        //查询我的权限 有没有增加 权限
        $AuthorAndChineseTeam = new AuthorAndChineseTeam($this->app);
        $MePermission_edit_add_user = $AuthorAndChineseTeam->Select( //判断我对这个文章有没有增加用户的权限
            $user_info["_id"],
            $postData['bookId'],
            ['edit_add_user', 'state', 'exp_state', 'author_or_chinese_team'],
        );
        if ($MePermission_edit_add_user['code'] == 200) {
            $MePermission_edit_add_user = $MePermission_edit_add_user['data'];
        } else {
            return $MePermission_edit_add_user;
        }
        //判断我的权限 有没有增加 权限 并且用户状态正常
        if (!$MePermission_edit_add_user['edit_add_user'] || $MePermission_edit_add_user['state'] == 1) {
            return json_decode(errorJsonReturn("AuthorAndChineseTeam_EnterAddUser-User-10001")->getContent(), true);
        }

        //查询要添加的用户 在不在当前项目中
        $isUserDataBase = Db::table('author_and_chinese_team')
            ->where([
                "book_id" =>  $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
            ])->count();
        if ($isUserDataBase != 0) {
            return json_decode(errorJsonReturn("AuthorAndChineseTeam_EnterAddUser-User-10002")->getContent(), true);
        }
        $insert_id = randIdStructure(1);
        if ($MePermission_edit_add_user["author_or_chinese_team"] == 0) { //不需要审核
            $insert_author_and_chinese_team = [ //要插入的数据
                "book_id" => $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "add_time" => date('Y-m-d H:i:s'),
                "insert_name_id" => $user_info["_id"],
                "_id" => $insert_id,
                "state" => 0,
                "exp_state" => null,
                "edit_delete_user" => 0, //删除权限
                "edit_inherit_users" => 0, //继承权限
                "edit_edit_user" => 0, //编辑权限
                "edit_add_user" => 0, //添加权限
            ];

            $log_book_id = $postData['bookId']; //书籍id
            $log_user_id = $postData["jsonDecodeUserInfo"]["user_id"]; //用户id
            $log_new_user_id = $postData["jsonDecodeUserInfo"]["user_id"]; //新用户id
            $log__id = $insert_id; //这个id 和 insert_author_and_chinese_team 里的id 一致
            $log_msg = "协作者 " . $user_info["_id"] . " 将用户 " . $postData["jsonDecodeUserInfo"]["user_id"] . " 加入了当前项目中 待审核"; //信息
            $log_state = 0; //执行状态 0 带执行 1已执行 2取消执行
            $log_apply_user_id = $user_info["_id"];
            $implement_time = date("Y-m-d H:i:s"); //执行时间
            $implement_user_id = $user_info["_id"]; //执行用户id
        } else { //需要审核
            $insert_author_and_chinese_team = [ //要插入的数据
                "book_id" => $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "add_time" => date('Y-m-d H:i:s'),
                "insert_name_id" => $user_info["_id"],
                "_id" => $insert_id,
                "state" => 1,
                "exp_state" => 11, //扩展状态 0协作者请求删除协作者 1代漫画作者审核 2被创作者删除 3转让自己的角色待审核 4已转让  5新加入待漫画作者审核 6漫画作者审核未通过 7自己退出项目 8移交的项目 9移入的用户 10被创作者申请踢出 11协作者邀请加入的用户
                "edit_delete_user" => 0, //删除权限
                "edit_inherit_users" => 0, //继承权限
                "edit_edit_user" => 0, //编辑权限
                "edit_add_user" => 0, //添加权限
            ];
            $log_book_id = $postData['bookId']; //书籍id
            $log_user_id = $postData["jsonDecodeUserInfo"]["user_id"]; //用户id
            $log_new_user_id = $postData["jsonDecodeUserInfo"]["user_id"]; //新用户id
            $log__id = $insert_id; //这个id 和 insert_author_and_chinese_team 里的id 一致
            $log_msg = "协作者 " . $user_info["_id"] . " 将用户 " . $postData["jsonDecodeUserInfo"]["user_id"] . " 加入了当前项目中 待审核"; //信息
            $log_state = 0; //执行状态 0 带执行 1已执行 2取消执行
            $log_apply_user_id = $user_info["_id"];
        }
        //插入数据 
        try {
            Db::table("author_and_chinese_team")
                ->insert($insert_author_and_chinese_team);
            $LogController = new LogController($this->app);
            $LogController->insert_AACT_log(
                $log_book_id,
                $log_user_id,
                $log_new_user_id,
                $log__id,
                $log_msg,
                $log_state,
                $log_apply_user_id,
                [
                    "implement_time" => $implement_time, //执行时间
                    "implement_user_id" => $implement_user_id, //执行者
                ]
            );
        } catch (\Throwable $th) {
            return errorJsonReturn('Failed to activate user user already in another state', '请刷新网页再试' . __LINE__); //!当用户已处于其他状态时候 
        }
    }
    /**
     * @方法名: 同意增加用户
     * @作者: 初雪桜
     * @信息: 协作者添加用户 作者同意
     * @param Json $user_info
     * @param Json $postData
     * @return Json
     */
    public function AllowAddUser($user_info, $postData)
    {
        //查询我的权限 有没有审核 权限
        $AuthorAndChineseTeam = new AuthorAndChineseTeam($this->app);
        $MePermission_edit_add_user = $AuthorAndChineseTeam->Select( //判断我对这个文章有没有增加用户的权限
            $user_info["_id"],
            $postData['bookId'],
            ['edit_add_user', 'state', 'exp_state', 'author_or_chinese_team'],
        );
        // $MePermission_edit_add_user = json_decode($MePermission_edit_add_user->getContent, true);
        if ($MePermission_edit_add_user['code'] == 200) {
            $MePermission_edit_add_user = $MePermission_edit_add_user['data'];
        } else {
            return $MePermission_edit_add_user;
        }
        //判断我的权限 有没有增加 权限 并且用户状态正常 或者我不是创作者 
        if (!$MePermission_edit_add_user['edit_add_user'] || $MePermission_edit_add_user['state'] == 1 || $MePermission_edit_add_user['author_or_chinese_team'] == 1) {
            return errorJsonReturn('Insufficient user rights'); //当前用户没有操作权限
        }
        $findUserDataBase = Db::table('author_and_chinese_team')
            ->where([
                "book_id" =>  $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "state" => 1,
                "exp_state" => 11,
            ])
            ->find();
        $updateUserDataBase = Db::table('author_and_chinese_team')
            ->where([
                "book_id" =>  $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "state" => 1,
                "exp_state" => 11,
            ])
            ->update([
                "exp_state" => null,
                "state" => 0,

            ]);
        if ($updateUserDataBase) {
            $LogController = new LogController($this->app);
            $LogController->update_aact_log(
                $postData['bookId'],
                $findUserDataBase["_id"],
                1,
                $user_info["_id"],
            );
        } else {
            return errorJsonReturn('Change failed this user is already in the group', '更改失败该用户已在本项目中'); //当前用户没有操作权限
        }
    }
    /**
     * @方法名: 拒绝添加用户
     * @作者: 初雪桜
     * @信息: 作者拒绝了协作者添加的用户
     * @param Json $user_info
     * @param Json $postData
     * @return Json
     */
    public function RefuseToJoin($user_info, $postData) //拒绝用户添加请求
    {
        //查询我的权限 有没有审核 权限
        $AuthorAndChineseTeam = new AuthorAndChineseTeam($this->app);
        $MePermission_edit_add_user = $AuthorAndChineseTeam->Select( //判断我对这个文章有没有增加用户的权限
            $user_info["_id"],
            $postData['bookId'],
            ['edit_add_user', 'state', 'exp_state', 'author_or_chinese_team'],
        );
        if ($MePermission_edit_add_user['code'] == 200) {
            $MePermission_edit_add_user = $MePermission_edit_add_user['data'];
        } else {
            return $MePermission_edit_add_user;
        }
        //判断我的权限 有没有增加 权限 并且用户状态正常 或者我不是创作者 
        if (!$MePermission_edit_add_user['edit_add_user'] || $MePermission_edit_add_user['state'] == 1 || $MePermission_edit_add_user['author_or_chinese_team'] == 1) {
            return errorJsonReturn('Insufficient user rights'); //当前用户没有操作权限
        }
        $findUserDataBase = Db::table('author_and_chinese_team')
            ->where([
                "book_id" =>  $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "state" => 1,
                "exp_state" => 11,
            ])
            ->find();
        $updateUserDataBase = Db::table('author_and_chinese_team')
            ->where([
                "book_id" =>  $postData['bookId'],
                "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                "author_or_chinese_team" => 1,
                "state" => 1,
                "exp_state" => 11,
            ])
            ->update([
                "exp_state" => 6,
                "is_deleted" => 1,
            ]);
        if ($updateUserDataBase) {
            $LogController = new LogController($this->app);
            $LogController->update_aact_log(
                $postData['bookId'],
                $findUserDataBase["_id"],
                2,
                $user_info["_id"],
            );
        } else {
            return errorJsonReturn('Change failed this user is already in the group', '更改失败该用户已在本项目中'); //当前用户没有操作权限
        }
    }
}
